package com.yuqing.sentimentapp.home.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.linkface.common.adaper.CommonViewHolder;
import com.linkface.common.adaper.MultiTypeSupport;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.home.bean.CommentItem;
import com.yuqing.sentimentapp.home.bean.ReplyBean;
import com.yuqing.sentimentapp.login.bean.LoginResultBean;
import com.yuqing.sentimentapp.utils.GlideUtil;
import com.yuqing.sentimentapp.utils.SharePrefrenceHelper;
import com.yuqing.sentimentapp.widget.CommentListView;
import com.yuqing.sentimentapp.widget.headeradapter.HeaderRecycleAdapter;

import java.util.List;

/**
 * Created by shenhe on 2018/9/19.
 */

public class YQDetailCommentAdapter extends HeaderRecycleAdapter<ReplyBean> implements MultiTypeSupport<ReplyBean> {
    private CommonViewHolder.onItemCommonClickListener commonClickListener;
    private Context mContext;

    public YQDetailCommentAdapter(Context context, List<ReplyBean> dataList) {
        super(context, dataList, R.layout.item_yq_detail_comment);
        this.mContext = context;
    }

    public YQDetailCommentAdapter(Context context, List<ReplyBean> dataList, CommonViewHolder.onItemCommonClickListener commonClickListener) {
        super(context, dataList, R.layout.item_yq_detail_comment);
        this.mContext = context;
        this.commonClickListener = commonClickListener;
        this.multiTypeSupport = this;
    }

    @Override
    public void bindData(final CommonViewHolder holder, final ReplyBean data) {
        holder.setText(R.id.tv_name, data.getWxName())
                .setText(R.id.tv_job, TextUtils.isEmpty(data.getPosition()) ? data.getCreateByDeptName() : data.getPosition())
                .setText(R.id.tv_content, data.getContent())
                .setText(R.id.tv_remark, "备注：" + data.getRemark())
                .setText(R.id.tv_time, data.getCreatedAt());
        holder.setViewVisibility(R.id.tv_remark, TextUtils.isEmpty(data.getRemark()) ? View.GONE : View.VISIBLE);
        CommentListView commentListView = holder.getView(R.id.commentList);
        List<CommentItem> comments = data.getComments();
        if (comments == null || comments.size() == 0) {
            commentListView.setVisibility(View.GONE);
        } else {
            commentListView.setVisibility(View.VISIBLE);
            commentListView.setDatas(comments);
        }
        GlideUtil.loadRoundImage(mContext, data.getHeadImg(), R.drawable.ic_default_head_detail, holder.getView(R.id.iv_head), 3);
        holder.setViewVisibility(R.id.btn_reply, View.VISIBLE);
        LoginResultBean.DataBean.UserBean user = SharePrefrenceHelper.getUser();
        if (user != null && user.getDeptVo() != null) {
            String deptLevel = user.getDeptVo().getDeptLevel();
            // 支队不显示回复按钮
            if (!TextUtils.isEmpty(deptLevel) && TextUtils.equals("2", deptLevel) || (TextUtils.equals(data.getCreateByDept(), user.getDeptCode()))) {
                holder.setViewVisibility(R.id.btn_reply, View.GONE);
            }
        }
        holder.getView(R.id.btn_reply).setOnClickListener(v -> {
            if (commonClickListener != null) {
                commonClickListener.onItemClickListener(holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getLayoutId(ReplyBean item, int position) {
        return R.layout.item_yq_detail_comment;
    }
}
